//
//  FHTabBar.swift
//  WeiBo
//
//  Created by qfh on 16/7/9.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

import SnapKit

class FHTabBar: UITabBar {
    
    /// 中间button 点击后执行的闭包
    var addCosuere: (()->())?
    
    /// tabbar 中间的添加按钮
    lazy var composeButton: UIButton = {
    
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
        
        button.addTarget(self, action: "addAction", forControlEvents: .TouchUpInside)
    
    
        return button
    }()

// MARK: - 初始或
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(composeButton)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 控件自动布局
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        /**
        *  总价button  自动布局
        */
        composeButton.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        // 遍历 下标
        var barBtnIndex = 0
        
        //每个tabBarButton 的宽度  共有5个
        let barBtnW = width / 5
        
        for value in subviews {
            
        
            //  获取子视图中的 UITabBarButton
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                            
                value.x = CGFloat(barBtnIndex) * barBtnW
                
                value.width = barBtnW
                

                barBtnIndex++
                
                // 空开中间的 add button
                if barBtnIndex == 2 {
                    barBtnIndex++
                }
                
                
            }
            
        }
        
        
        
    }
    
    /**
     中间button的  点击事件
     */
    
    @objc private func addAction() {
    
    
        addCosuere?()
    }
   
    
    

}
